/**
 * Copyright 2011 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.google.solarchallenge.client.gwtui.widgets.useraccountdetails;

import com.google.gwt.core.client.GWT;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;
import com.google.solarchallenge.client.gwtui.mvpinterfaces.View;
import com.google.solarchallenge.client.gwtui.widgets.solarchallengeheaderbar.SolarChallengeHeaderBarDisplay;
import com.google.solarchallenge.shared.LoginCategory;
import com.google.solarchallenge.shared.dtos.UserAccountDto;

/**
 * View for UserAccountDetails.
 *
 * @author Arjun Satyapal
 */
public class UserAccountDetailsView extends View implements
    UserAccountDetailsDisplay {
  @UiField
  TextBox textBoxFirstName;
  @UiField
  TextBox textBoxLastName;
  @UiField
  ListBox listBoxLoginCategory;
  @UiField
  TextBox textBoxEmail;
  @UiField
  CheckBox checkboxIsLoginAllowed;

  private static ApproverViewUiBinder uiBinder = GWT
      .create(ApproverViewUiBinder.class);

  interface ApproverViewUiBinder extends
      UiBinder<Widget, UserAccountDetailsView> {
  }

  public UserAccountDetailsView() {
    initWidget(uiBinder.createAndBindUi(this));
    refreshLoginCategory();
    checkboxIsLoginAllowed.setVisible(false);
  }

  @Override
  public SolarChallengeHeaderBarDisplay getSolarChallengeHeaderPanel() {
    return null;
  }

  @Override
  public TextBox getTextBoxFirstName() {
    return textBoxFirstName;
  }

  @Override
  public TextBox getTextBoxLastName() {
    return textBoxLastName;
  }

  @Override
  public UserAccountDto getValuesAsDto() {
    LoginCategory loginCategory = LoginCategory.valueOf(listBoxLoginCategory
        .getItemText(listBoxLoginCategory.getSelectedIndex()));
    return new UserAccountDto.Builder()
        .setFirstName(textBoxFirstName.getText())
        .setLastName(textBoxLastName.getText())
        .setEmail(textBoxEmail.getText()).setLoginCategory(loginCategory)
        .setIsLoginAllowed(checkboxIsLoginAllowed.getValue()).build();
  }

  @Override
  public void updateValuesFromDto(UserAccountDto dto) {
    textBoxFirstName.setText(dto.getFirstName());
    textBoxLastName.setText(dto.getLastName());
    textBoxEmail.setText(dto.getEmail());
    listBoxLoginCategory.clear();
    listBoxLoginCategory.addItem(dto.getLoginCategory().name());
    listBoxLoginCategory.setSelectedIndex(0);
    listBoxLoginCategory.setEnabled(false);
    checkboxIsLoginAllowed.setValue(dto.isLoginAllowed());
  }

  @Override
  public void refreshLoginCategory() {
    listBoxLoginCategory.clear();
    for (LoginCategory curr : LoginCategory.values()) {
      if (curr == LoginCategory.GUEST) {
        continue;
      }

      listBoxLoginCategory.addItem(curr.name());
    }
    listBoxLoginCategory.setSelectedIndex(0);
  }

  @Override
  public void selectLoginCategory(LoginCategory loginCategory) {
    if (loginCategory == LoginCategory.GUEST) {
      Window.alert("Invalid Category Guest.");
    } else {
      for (int i = 0; i < listBoxLoginCategory.getItemCount(); i++) {
        if (listBoxLoginCategory.getItemText(i).equals(loginCategory.name())) {
          listBoxLoginCategory.setSelectedIndex(i);
          break;
        }
      }
    }
  }

  @Override
  public void setEnabled(boolean enabled) {
    textBoxFirstName.setEnabled(enabled);
    textBoxLastName.setEnabled(enabled);
    textBoxEmail.setEnabled(enabled);
    listBoxLoginCategory.setEnabled(enabled);
    checkboxIsLoginAllowed.setEnabled(enabled);
  }

  @Override
  public ListBox getListBoxLoginCategory() {
    return listBoxLoginCategory;
  }

  @Override
  public TextBox getTextBoxEamil() {
    return textBoxEmail;
  }

  @Override
  public CheckBox getCheckBoxIsLoginAllowed() {
    return checkboxIsLoginAllowed;
  }
}
